﻿using System;
using System.Collections.Generic;
using System.Linq;

// 学生对象
class Student
{
    public string Id { get; set; }
    public string Name { get; set; }

    public void SignIn(Course course, AttendanceSystem system)
    {
        Console.WriteLine($"[学生操作] {Name} 正在为课程《{course.Name}》签到...");
        system.RecordAttendance(this, course);
    }
}

// 课程对象
class Course
{
    public string Id { get; set; }
    public string Name { get; set; }
    public DateTime StartTime { get; set; }

    public bool IsOpen()
    {
        TimeSpan diff = DateTime.Now - StartTime;
        return diff.TotalMinutes >= -30 && diff.TotalMinutes <= 30;
    }

    public void DisplayInfo()
    {
        Console.WriteLine($"课程: {Name}, 开始时间: {StartTime:yyyy-MM-dd HH:mm}");
    }
}

// 签到记录对象
class AttendanceRecord
{
    public Student Student { get; set; }
    public Course Course { get; set; }
    public DateTime SignInTime { get; set; }
    public string Status { get; set; }

    public void CalculateStatus()
    {
        TimeSpan diff = SignInTime - Course.StartTime;

        if (diff.TotalMinutes <= 10)
            Status = "准时";
        else if (diff.TotalMinutes <= 30)
            Status = "迟到";
        else
            Status = "严重迟到";
    }

    public void Display()
    {
        Console.WriteLine($"  → {Student.Name} 于 {SignInTime:HH:mm:ss} 签到，状态：{Status}");
    }
}

// 签到系统对象
class AttendanceSystem
{
    private List<AttendanceRecord> records = new List<AttendanceRecord>();

    public void RecordAttendance(Student student, Course course)
    {
        if (!course.IsOpen())
        {
            Console.WriteLine($"  ✗ 签到失败：课程《{course.Name}》未开放签到");
            return;
        }

        bool alreadySigned = records.Any(r =>
            r.Student.Id == student.Id && r.Course.Id == course.Id);

        if (alreadySigned)
        {
            Console.WriteLine($"  ✗ 签到失败：{student.Name} 已经签到过了");
            return;
        }

        AttendanceRecord record = new AttendanceRecord
        {
            Student = student,
            Course = course,
            SignInTime = DateTime.Now
        };

        record.CalculateStatus();
        records.Add(record);
        Console.WriteLine($"  ✓ 签到成功！");
    }

    public void ShowCourseAttendance(Course course)
    {
        Console.WriteLine($"\n========== 《{course.Name}》签到情况 ==========");

        var courseRecords = records.Where(r => r.Course.Id == course.Id).ToList();

        if (courseRecords.Count == 0)
        {
            Console.WriteLine("  暂无签到记录");
        }
        else
        {
            foreach (var record in courseRecords)
            {
                record.Display();
            }
        }

        Console.WriteLine($"总计: {courseRecords.Count} 人已签到\n");
    }

    public void ShowStudentHistory(Student student)
    {
        Console.WriteLine($"\n========== {student.Name} 的签到历史 ==========");

        var studentRecords = records.Where(r => r.Student.Id == student.Id).ToList();

        if (studentRecords.Count == 0)
        {
            Console.WriteLine("  暂无签到记录");
        }
        else
        {
            foreach (var record in studentRecords)
            {
                Console.WriteLine($"  课程：{record.Course.Name} | 时间：{record.SignInTime:MM-dd HH:mm} | 状态：{record.Status}");
            }
        }

        Console.WriteLine($"总计: {studentRecords.Count} 次签到\n");
    }
}

// 主程序
class Program
{
    static void Main()
    {
        // ✅ 重要：设置控制台编码为UTF-8，支持中文显示
        Console.OutputEncoding = System.Text.Encoding.UTF8;

        Console.WriteLine("======================================");
        Console.WriteLine("     面向对象：上课签到系统演示");
        Console.WriteLine("======================================\n");

        // 创建课程对象
        Course mathCourse = new Course
        {
            Id = "C001",
            Name = "高等数学",
            StartTime = DateTime.Now.AddMinutes(-5)
        };

        Course englishCourse = new Course
        {
            Id = "C002",
            Name = "大学英语",
            StartTime = DateTime.Now.AddMinutes(40)
        };

        // 创建学生对象
        Student alice = new Student { Id = "S001", Name = "张小明" };
        Student bob = new Student { Id = "S002", Name = "李华" };
        Student charlie = new Student { Id = "S003", Name = "王芳" };

        // 创建签到系统
        AttendanceSystem system = new AttendanceSystem();

        Console.WriteLine(">>> 场景1：学生为数学课签到\n");
        mathCourse.DisplayInfo();
        Console.WriteLine();

        // 学生签到
        alice.SignIn(mathCourse, system);
        bob.SignIn(mathCourse, system);

        // 模拟延迟
        System.Threading.Thread.Sleep(1000);
        charlie.SignIn(mathCourse, system);

        // 演示重复签到
        Console.WriteLine();
        alice.SignIn(mathCourse, system);

        // 查看课程签到情况
        system.ShowCourseAttendance(mathCourse);

        Console.WriteLine("\n>>> 场景2：尝试为未开放的课程签到\n");
        englishCourse.DisplayInfo();
        Console.WriteLine();
        alice.SignIn(englishCourse, system);

        // 查看学生签到历史
        system.ShowStudentHistory(alice);
        system.ShowStudentHistory(bob);

        Console.WriteLine("\n======================================");
        Console.WriteLine("演示完成！观察对象如何协作完成任务");
        Console.WriteLine("======================================");

        // 防止控制台窗口立即关闭
        Console.WriteLine("\n按任意键退出程序...");
        Console.ReadKey();
    }
}